<?php

/**
 * 分数结算 房间重置方法
 */


//获取房间用户信息
function getRoomUser()
{
    $mysql = new mysqli_connect('127.0.0.1', 'root', '', 'pigscanfly');
    $mysql->query('set names utf8');
    $sql = 'select * from users where 1';
    $info = $mysql->query($sql);
}


/**
 * 房间重置方法
 * @param  [string] $roomid [传入房间ID]
 */
function resetRoom($roomid)
{
    $GLOBALS['hall'][$roomid]['pokers'] = [];
    $GLOBALS['hall'][$roomid]['status'] = 'off';
    $GLOBALS['hall'][$roomid]['whosturn'] = '';

    $GLOBALS['hall'][$roomid]['user_a_score'] = 0;
    $GLOBALS['hall'][$roomid]['user_b_score'] = 0;
    $GLOBALS['hall'][$roomid]['user_c_score'] = 0;

    $GLOBALS['hall'][$roomid]['user_a_pokers'] = [];
    $GLOBALS['hall'][$roomid]['user_b_pokers'] = [];
    $GLOBALS['hall'][$roomid]['user_c_pokers'] = [];

    $GLOBALS['hall'][$roomid]['user_a_status'] = '空闲';
    $GLOBALS['hall'][$roomid]['user_b_status'] = '空闲';
    $GLOBALS['hall'][$roomid]['user_c_status'] = '空闲';
}


/**
 * [游戏结束 结算方法]
 * @param  [string] $roomid [房间的ID]
 * @param  [array]  $type   [可选参数 如果有玩家断线或逃跑 传入 ['ranout'=>'玩家昵称'] ]
 * @return [array]          [返回输赢数组]
 */
function gameOver($roomid, $type=[])
{   
    $user_a_score = $GLOBALS['hall'][$roomid]['user_a_score'];
    $user_b_score = $GLOBALS['hall'][$roomid]['user_b_score'];
    $user_c_score = $GLOBALS['hall'][$roomid]['user_c_score'];

    $max = max($user_a_score,$user_b_score,$user_c_score);
    $min = min($user_a_score,$user_b_score,$user_c_score);
    $win = [];  $lose = [];

    $pokers = [
        'user_a_pokers' => $GLOBALS['hall'][$roomid]['user_a_pokers'],
        'user_b_pokers' => $GLOBALS['hall'][$roomid]['user_b_pokers'],
        'user_c_pokers' => $GLOBALS['hall'][$roomid]['user_c_pokers'],
    ];

    //如果是有人逃跑 直接返回结果
    if (isset($type['ranout'])) {
        resetRoom($roomid);
        return ['win'=>$win,'lose'=>[$GLOBALS['hall'][$roomid]['user_a_nick']], 'ranout'=>$type['ranout']];
    }
    
    //如果玩家的分数完全一样 则直接返回
    if ($user_a_score == $user_b_score && $user_b_score == $user_c_score) {
        return ['win'=>$win,'lose'=>$lose,'pokers'=>$pokers];

    //如果有两个玩家的分数是一样的
    } else if ($user_a_score == $user_b_score) {
        if ($user_a_score == $max) {
            $win =  [
                        [$GLOBALS['hall'][$roomid]['user_a_nick'], $pokers['user_a_pokers']],
                        [$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']],
                    ];
            $lose = [[$GLOBALS['hall'][$roomid]['user_c_nick'], $pokers['user_c_pokers']]];
        } else if ($user_a_score == $min) {
            $lose = [
                        [$GLOBALS['hall'][$roomid]['user_a_nick'], $pokers['user_a_pokers']],
                        [$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']],
                    ];
            $win =  [[$GLOBALS['hall'][$roomid]['user_c_nick'], $pokers['user_c_pokers']]];
        }
    } else if ($user_a_score == $user_c_score) {
        if ($user_c_score == $max) {
            $win =  [
                        [$GLOBALS['hall'][$roomid]['user_a_nick'], $pokers['user_a_pokers']],
                        [$GLOBALS['hall'][$roomid]['user_c_nick'], $pokers['user_c_pokers']],
                    ];
            $lose = [[$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']]];
        } else if ($user_a_score == $min) {
            $lose = [
                        [$GLOBALS['hall'][$roomid]['user_a_nick'],$pokers['user_a_pokers']],
                        [$GLOBALS['hall'][$roomid]['user_c_nick'],$pokers['user_c_pokers']],
                    ];
            $win =  [[$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']]];
        }
    } else if ($user_b_score == $user_c_score) {
        if ($user_b_score == $max) {
            $win =  [
                        [$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']], 
                        [$GLOBALS['hall'][$roomid]['user_c_nick'], $pokers['user_c_pokers']],
                    ];
            $lose = [[$GLOBALS['hall'][$roomid]['user_a_nick'], $pokers['user_a_pokers']]];
        } else if ($user_b_score == $min) {
            $lose = [
                        [$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']], 
                        [$GLOBALS['hall'][$roomid]['user_c_nick'], $pokers['user_c_pokers']],
                    ];
            $win =  [[$GLOBALS['hall'][$roomid]['user_a_nick'], $pokers['user_a_pokers']]];
        }

    //如果玩家的分数都不一样
    } else {
        if ($max == $user_a_score) {
            $win =  [[$GLOBALS['hall'][$roomid]['user_a_nick'], $pokers['user_a_pokers']]];
            $lose = [
                        [$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']], 
                        [$GLOBALS['hall'][$roomid]['user_c_nick'], $pokers['user_c_pokers']],
                    ];
        } else if ($max == $user_b_score) {
            $win =  [[$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']]];
            $lose = [
                        [$GLOBALS['hall'][$roomid]['user_a_nick'],$pokers['user_a_pokers']], 
                        [$GLOBALS['hall'][$roomid]['user_c_nick'],$pokers['user_c_pokers']],
                    ];
        } else {
            $lose = [
                        [$GLOBALS['hall'][$roomid]['user_a_nick'], $pokers['user_a_pokers']], 
                        [$GLOBALS['hall'][$roomid]['user_b_nick'], $pokers['user_b_pokers']],
                    ];
            $win = [[$GLOBALS['hall'][$roomid]['user_c_nick'], $pokers['user_c_pokers']]];
        }
    }

    //重置房间所有属性
    resetRoom($roomid);
    //返回胜负数据给玩家
    return ['win'=>$win,'lose'=>$lose,'pokers'=>$pokers];
}
